let app = getApp();
let sourceType = [
  ['camera'],
  ['album'],
  ['camera', 'album']
]
let sizeType = [
  ['compressed'],
  ['original'],
  ['compressed', 'original']
]

Page({
  data: {
    imageList: [],
    openid: '',
    comment: '',
    location: '',
    sourceTypeIndex: 2,
    sourceType: ['拍照', '相册', '拍照或相册'],
    sizeTypeIndex: 0,
    sizeType: ['压缩', '原图', '压缩或原图'],
    countIndex: 0, 
    count: [1],
    customerid: 0,
    customerState: ['A', 'B', 'C', 'D'],
    Rankid: 0,
    customerRank: ['A', 'B', 'C', 'D'],
  },
  onLoad() {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success(res) {
        that.setData({
          openid: res.data
        });
      }
    });
    this.getLocation();
  },
  //拿到备注值
  comment(res) {
    this.setData({
      comment: res.detail.value
    });
  },

  StateChange(e) {
    this.setData({
      customerid: e.detail.value
    })
  },
  RankChange(e) {
    this.setData({
      Rankid: e.detail.value
    })
  },
  chooseImage() {
    let that = this
    wx.chooseImage({
      sourceType: sourceType[this.data.sourceTypeIndex],
      sizeType: sizeType[this.data.sizeTypeIndex],
      count: this.data.count[this.data.countIndex],
      success(res) {        
        if (res.tempFiles[0].size <= 2000000) {          
          that.setData({
            imageList: res.tempFilePaths
          })
        } else {
          app.toast('图片过大,请重新选择');
          return false;
        }
      }
    })
  },
  previewImage(e) {
    let current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  //提交考勤
  addSingIn() {
    let that = this,
      comment = that.data.comment,
      imgaeUrl = that.data.imageList,   
      location = that.data.location;
    if (!comment) {
      app.toast('备注不可为空');
    } else if (imgaeUrl.length==0) {
      app.toast('请上传图片');      
    } else if (!location) {
      app.toast('地址不可为空');
    } else {
     let imageBase64 = wx.getFileSystemManager().readFileSync(imgaeUrl[0], "base64");
      app.post('KaoQin', {
        action: 'insert',
        openid: that.data.openid,
        BeiZhu: comment,
        DiDian: location,
        ZhaoPianbase64: imageBase64
      }).then((res) => {
        if (res.msg === '考勤成功！') {
          app.toast('添加签到成功');
          wx.redirectTo({
            url: '/pages/signInList/signInList'
          });
        } else {
          app.toast('添加签到失败');
        }
      });
    }
  },
  Check() {
    this.getLocation();
  },
  getLocation() {
    wx.showLoading({
      title: '获取位置中...',
    });
    let that = this;
    wx.getLocation({ //调用API得到经纬度
      type: 'wgs84',
      success(res) {
       app.get('DiZhi',{
         jingdu: res.latitude,
         weidu: res.longitude
       }).then((data)=>{
         wx.hideLoading();
         app.toast('获取位置成功');
         that.setData({location:data.dizhi});
       });      
      }
    })
  }
})